<?php

namespace App\Models;

use App\Common\Traits\HasAgentRevenues;

class Order extends Base
{
    use HasAgentRevenues;

    /**
     * 发货状态: 待发货
     */
    const STATUS_TO_BE_DELIVERED = 1;

    /**
     * 发货状态: 已发货
     */
    const STATUS_SHIPPED = 2;

    /**
     * 发货状态: 已取消
     */
    const STATUS_CANCELLED = 3;

    const STATUS_MAP = [
        self::STATUS_TO_BE_DELIVERED => '待发货',
        self::STATUS_SHIPPED => '已发货',
        self::STATUS_CANCELLED => '已取消',
    ];

    /**
     * 取消类型: 代理商取消
     */
    const CANCEL_TYPE_AGENT = 1;

    /**
     * 取消类型: 管理员取消
     */
    const CANCEL_TYPE_ADMIN = 2;

    /**
     * 已失效: 是
     */
    const IS_EXPIRED_TRUE = 1;

    /**
     * 已失效: 否
     */
    const IS_EXPIRED_FALSE = 2;

    /**
     * 下单统计 Job 结算状态: 1:未完成
     */
    const SETTLEMENT_STATUS_UNDONE = 1;

    /**
     * 下单统计 Job 结算状态: 2:已完成
     */
    const SETTLEMENT_STATUS_COMPLETED = 2;

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'cancel_time' => 'datetime:Y-m-d H:i:s',
        'commodity_snapshot' => 'array',
        'commodity_activity_snapshot' => 'array',
        'agent_snapshot' => 'array',
        'client_snapshot' => 'array',
        'direct_inviter_snapshot' => 'array',
        'indirect_inviter_snapshot' => 'array',
        'reward_setting_snapshot' => 'array',
    ];

    /**
     * 订单 (懒加载用, 避免在连表时候 .* 导致临时表暴增)
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function orderDetail()
    {
        return $this->belongsTo(Order::class, 'id', 'id');
    }

    /**
     * 代理商的订单
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function agent()
    {
        return $this->belongsTo(Agent::class);
    }

    /**
     * 直接邀请人
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function directInviter()
    {
        return $this->belongsTo(Agent::class, 'direct_inviter');
    }

    /**
     * 间接邀请人
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function indirectInviter()
    {
        return $this->belongsTo(Agent::class, 'indirect_inviter');
    }

    /**
     * 这条订单产生的活动奖励
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function agentReward()
    {
        return $this->morphMany(AgentReward::class, 'source');
    }

    /**
     * 订单的 sku 明细
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function skus()
    {
        return $this->hasMany(OrderSku::class);
    }

    /**
     * 订单的售后订单
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function afterSaleOrder()
    {
        return $this->hasMany(AfterSaleOrder::class);
    }

    /**
     * 供应商
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }

    /**
     * 快递信息
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function courierInformation()
    {
        return $this->morphMany(CourierInformation::class, 'deliverable');
    }

    /**
     * 商品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function commodity()
    {
        return $this->belongsTo(Commodity::class)->withTrashed();
    }

    /**
     * 商品活动
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function commodityActivity()
    {
        return $this->belongsTo(CommodityActivity::class);
    }
}
